//
//  RequsetManager.m
//  SenMo
//
//  Created by ZengYuan on 15/12/31.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "RequsetManager.h"

#define KRequset @"KRequset"

@implementation RequsetManager

+(NSArray *)getLocalRequest:(NSString *)device_id{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *wdata = [defaults objectForKey:[KRequset stringByAppendingString:device_id]];
    NSArray *result = [NSKeyedUnarchiver unarchiveObjectWithData:wdata];
    return result;
}

+(BOOL)addNewRequest:(NSString *)device_id
                  request:(NSArray *)array{
    if (!array)
    {
        return NO;
    }
    
    //先加最新的
    NSMutableArray *new = [NSMutableArray array];
    [new addObjectsFromArray:array];
    
   //再加以前的
    NSArray *local = [self getLocalRequest:device_id];
    if(local){
        [new addObjectsFromArray:local];
    }
    
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *_data = [NSKeyedArchiver archivedDataWithRootObject:new];
    [defaults setObject:_data forKey:[KRequset stringByAppendingString:device_id]];
    
    return YES;
}


+(BOOL)resetLocalRequest:(NSString *)device_id
                 request:(NSArray *)array{
    

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if (!array) {
        [defaults removeObjectForKey:[KRequset stringByAppendingString:device_id]];
        return YES;
    }
    
    
    NSData *_data = [NSKeyedArchiver archivedDataWithRootObject:array];
    [defaults setObject:_data forKey:[KRequset stringByAppendingString:device_id]];
    [defaults synchronize];
    return YES;
}

@end
